package com.example.adms.common.mail;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 邮件消费者
 */
@Component
@Slf4j
public class MailConsumer {

    @Autowired
    private MailService mailService;

    @KafkaListener(topics = "mail")
    public void handleMail(ConsumerRecord record){
        if (record == null || record.value() == null){
            log.error("消息为空");
            return;
        }
        // 从消息队列中取出消息
        Mail mail = JSONObject.parseObject(record.value().toString(), Mail.class);
        if (mail == null){
            log.error("格式错误");
            return;
        }
        mailService.sendSimpleMail(mail);
    }

}
